import 'package:flutter/material.dart';

enum XDSheetActionStyle {
   Default,
   Cancel,
   Destructive
}
class XDSheetAction{
  String title;
  XDSheetActionStyle style;
  Function(XDSheetAction action) handler;
  XDSheetAction({
    Key? key,
    required this.title,
    this.style = XDSheetActionStyle.Default,
    required this.handler,
  });
}
class XDSheetActionCell extends StatelessWidget {
  XDSheetAction action;
  XDSheetActionCell({
    Key? key,
    required this.action,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    return InkWell(
      onTap: (){
        action.handler(action);
        Navigator.of(context).pop();
      },
      child: Container(
        width: screenWidth,
        child: Column(
          children: [
            Container(
              width: screenWidth,
              height: (action.style == XDSheetActionStyle.Cancel ) ? 6 : 1,
              color: Color((action.style == XDSheetActionStyle.Cancel ) ? 0xFFF8F8F7 : 0xFFF1F1F1),
            ),

            Container(
              width: screenWidth,
              height: 44,
              color: Colors.white,
              alignment: Alignment.center,
              child: Text(action.title,
                style: TextStyle(
                  fontSize: 16,
                  color: Color((action.style == XDSheetActionStyle.Cancel ) ? 0xFF000000 : (action.style == XDSheetActionStyle.Destructive) ? 0xFFFB1B1B : 0xFF2F2F2F)
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
class XDSheetHeader extends StatelessWidget {
  String? title;
  String? headline;
  String? subTitle;
  XDSheetHeader({
    Key? key,
    this.title,
    this.headline,
    this.subTitle,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    return Container(
      width: screenWidth,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(8)),
      ),
      child: Column(
         children: [
           Offstage(
             offstage: title == null,
             child: Container(
               width: screenWidth,

               margin: EdgeInsets.only(top: 18,left: 20,right: 20,bottom: (headline == null && subTitle == null) ? 26 : 0),
               alignment: Alignment.center,
               child: Text(
                 title ?? '',
                 style: TextStyle(
                   fontSize: 16,
                   color: Color(0xFF2F2F2F),
                 ),
               ),
             ),
           ),
           Offstage(
             offstage: headline == null,
             child: Container(
               width: screenWidth,
               margin: EdgeInsets.only(top: 18,left: 20,right: 20,bottom: (subTitle == null) ? 26 : 0),
               alignment: Alignment.center,
               child: Text(
                 headline ?? '',
                 style: TextStyle(
                   fontSize: 13,
                   color: Color(0xFF323232),
                 ),
                 textAlign: TextAlign.center,
               ),
             ),
           ),

           Offstage(
             offstage: subTitle == null,
             child: Container(
               width: screenWidth,
               margin: EdgeInsets.only(top: (headline == null) ? 18 : 5,bottom: 26,left: 20,right: 20),
               alignment: Alignment.center,
               child: Text(
                 subTitle ?? '',
                 style: TextStyle(
                   fontSize: 12,
                   color: Color(0xFF6A7081),
                 ),
                 textAlign: TextAlign.center,
               ),
             ),
           ),
         ],
      ),
    );
  }
}



class XDBottomSheet extends StatelessWidget {
  String? title;
  String? headline;
  String? subTitle;
  List? actions;
  XDBottomSheet({
    Key? key,
    this.title,
    this.headline,
    this.subTitle,
    this.actions}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<Widget> actionCells = [];

    if(title != null || headline != null || subTitle != null) {
      Widget header = XDSheetHeader(title: title,headline: headline,subTitle: subTitle,);
      actionCells.add(header);
    }
    int i = 0;
    while(actions != null && i < actions!.length){
      XDSheetAction action = actions![i];
      actionCells.add(XDSheetActionCell(action: action!,));
      i++;
    }
    actionCells.add(Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).padding.bottom,
      color: Color(0xFFF8F8F7),
    ));
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: actionCells ?? [],
    );

  }
}

